package com.haixiaoke.saas.house.service.impl;

import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.house.domain.HouseChargeItem;
import com.haixiaoke.saas.house.mapper.HouseChargeItemMapper;
import com.haixiaoke.saas.house.service.IHouseChargeItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 房屋收费项目Service业务层处理
 *
 * @author xiaolong
 * @date 2023-11-25
 */
@Service
public class HouseChargeItemServiceImpl implements IHouseChargeItemService {
    @Autowired
    private HouseChargeItemMapper houseChargeItemMapper;

    /**
     * 查询房屋收费项目
     *
     * @param chargeItemId 房屋收费项目主键
     * @return 房屋收费项目
     */
    @Override
    public HouseChargeItem selectHouseChargeItemByChargeItemId(Long chargeItemId) {
        return houseChargeItemMapper.selectHouseChargeItemByChargeItemId(chargeItemId);
    }

    /**
     * 查询房屋收费项目列表
     *
     * @param houseChargeItem 房屋收费项目
     * @return 房屋收费项目
     */
    @Override
    public List<HouseChargeItem> selectHouseChargeItemList(HouseChargeItem houseChargeItem) {
        return houseChargeItemMapper.selectHouseChargeItemList(houseChargeItem);
    }

    /**
     * 新增房屋收费项目
     *
     * @param houseChargeItem 房屋收费项目
     * @return 结果
     */
    @Override
    public int insertHouseChargeItem(HouseChargeItem houseChargeItem) {
        houseChargeItem.setCreateTime(DateUtils.getNowDate());
        return houseChargeItemMapper.insertHouseChargeItem(houseChargeItem);
    }

    /**
     * 修改房屋收费项目
     *
     * @param houseChargeItem 房屋收费项目
     * @return 结果
     */
    @Override
    public int updateHouseChargeItem(HouseChargeItem houseChargeItem) {
        HouseChargeItem byChargeItemId = houseChargeItemMapper.selectHouseChargeItemByChargeItemId(houseChargeItem.getChargeItemId());
        if (!TenantContext.getTenantId().equals(byChargeItemId.getTenantId())) {
            return 0;
        }

        houseChargeItem.setUpdateTime(DateUtils.getNowDate());
        return houseChargeItemMapper.updateHouseChargeItem(houseChargeItem);
    }

    /**
     * 批量删除房屋收费项目
     *
     * @param chargeItemIds 需要删除的房屋收费项目主键
     * @return 结果
     */
    @Override
    public int deleteHouseChargeItemByChargeItemIds(Long[] chargeItemIds) {
        HouseChargeItem houseChargeItem = new HouseChargeItem();
        houseChargeItem.setTenantId(TenantContext.getTenantId());
        houseChargeItem.setChargeItemIds(chargeItemIds);
        List<HouseChargeItem> houseChargeItemList = houseChargeItemMapper.selectHouseChargeItemByChargeItemIds(houseChargeItem);
        if (houseChargeItemList.size() < chargeItemIds.length) {
            return 0;
        }

        return houseChargeItemMapper.deleteHouseChargeItemByChargeItemIds(chargeItemIds);
    }

    /**
     * 删除房屋收费项目信息
     *
     * @param chargeItemId 房屋收费项目主键
     * @return 结果
     */
    @Override
    public int deleteHouseChargeItemByChargeItemId(Long chargeItemId) {
        return houseChargeItemMapper.deleteHouseChargeItemByChargeItemId(chargeItemId);
    }
}
